package dtxt

import com.dtxt.ListToGrid
import com.dtxt.MD5Util
import grails.converters.JSON
import grails.transaction.Transactional
import groovy.sql.GroovyRowResult

import static org.springframework.http.HttpStatus.NOT_FOUND;

import groovy.sql.Sql;


class AdminController {
    def dataSource

    def login() {
        render view: "login"
    }

    def signIn(String username,String password){
        Sql sql = new Sql(dataSource: dataSource)
         username = params.username
         password =MD5Util.MD5(params.password)
          String sqlList ="select count(*) c from sys_user where login_name='${username}' and password='${password}' and admin_lock=0"
        def list = sql.firstRow(sqlList)
        if (list.c == 1){
            render "true"
        }else{
            render "false"
        }
    }



    def main(){
        render view: "main"
    }


    def dtxm_json(){
        Sql sql = new Sql(dataSource: dataSource)
        String xmSqlList = "select id,PROJECT_NAME,ISENABLE from dt_xm "
        String sqlCount = """select count(*) c from (${xmSqlList}) t"""
        String listcount = sql.firstRow(sqlCount).c
        List<GroovyRowResult> list = sql.rows(xmSqlList)
          String json_jg =ListToGrid.formatList(list,listcount)
        render json_jg
    }

    @Transactional
    def dt_xmadd(Dt_xm dt_xmInstance) {
        if (dt_xmInstance == null) {
            notFound()
            return
        }
        Sql sql = new Sql(dataSource: dataSource)
        String xmName = params?.projectName
        String xmSql = "select count(*) c from dt_xm  where project_Name = '${xmName}' "

        int sqlCount = sql.firstRow(xmSql).c
        log.info(sqlCount)
        if (sqlCount >= 1) {
         render "1"   //有重名项目
        } else {
            try {
                dt_xmInstance.save flush: true
                render true
            } catch (Exception e) {
                e.printStackTrace()
                render false
            }
        }
    }
    @Transactional
    def del_xm(String id){
        int delid = Integer.parseInt(id)
        Sql sql = new Sql(dataSource: dataSource)
        String delSql = """delete from dt_xm where id=${delid}"""
        sql.execute(delSql)
         render "true"
    }

    @Transactional
    def update(Dt_xm dt_xmInstance) {
        log.info(dt_xmInstance)
        if (dt_xmInstance == null) {
            notFound()
            return
        }

        try {
            dt_xmInstance.save flush: true
            render true
        } catch (Exception e) {
            e.printStackTrace()
            render false
        }
    }

    @Transactional
    def Edit_xm() {
        Dt_xm vender = Dt_xm.get(params?.id)
        render vender as JSON
    }
    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'dt_xm.label', default: 'Dt_xm'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }


    def dt_xm(){
        render view: "dt_xm"
    }


}
